package com.webuy.h5.container

import okhttp3.CacheControl
import okhttp3.Interceptor
import okhttp3.Response
import timber.log.Timber
import java.io.IOException
import java.util.concurrent.TimeUnit

/**
 * author: luoXiong
 * e-mail: 382060748@qq.com
 * date: 2021/7/22 11:18
 * version: 1.0
 * desc:
 */
class MyOkHttpCacheInterceptor : Interceptor {
    private var maxAga = 365 //default
    private var timeUnit = TimeUnit.DAYS
    fun setMaxAge(maxAga: Int, timeUnit: TimeUnit): MyOkHttpCacheInterceptor {
        this.maxAga = maxAga
        this.timeUnit = timeUnit
        return this
    }

    override fun intercept(chain: Interceptor.Chain): Response {
        val req=chain.request()
        Timber.d("网络拦截器...intercept()...111..url=${req.url.toString()}")
        val response: Response = chain.proceed(req)
        val cacheControl: CacheControl = CacheControl.Builder()
            .maxAge(maxAga, timeUnit)
            .build()
        return response.newBuilder()
            .removeHeader("Pragma")
            .removeHeader("Cache-Control")
            .header("Cache-Control", cacheControl.toString())
            .build()
    }
}